//
//  ZTFavorViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTFavorViewController.h"
#import "ZTFavorTableViewCell.h"
#import "AppTools.h"
#import "ZTFavorModel.h"
#import "ZTAlbumDetailViewController.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ZTFavorViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *favorTableView;

@property (nonatomic, strong) NSMutableArray *recommendArray;

@property (nonatomic, strong) NSMutableArray *myAlbumArray;

@end

@implementation ZTFavorViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.myAlbumArray = [NSMutableArray arrayWithArray:[[ZHFMDB shareFMDB] selectAlbumData]];
    [self.favorTableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];



#pragma mark - 创建tableView
    self.favorTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20 - 40 - 44) style:UITableViewStyleGrouped];
    [self.view addSubview:self.favorTableView];
    self.favorTableView.rowHeight = WIDTH * 0.3;
    self.favorTableView.backgroundColor = [UIColor whiteColor];
    self.favorTableView.dataSource = self;
    self.favorTableView.delegate = self;
    
    [self.favorTableView registerClass:[ZTFavorTableViewCell class] forCellReuseIdentifier:@"favorTableCell"];
    self.favorTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    

    [self.favorTableView reloadData];
    self.recommendArray = [NSMutableArray array];
    [self getData];

}


#pragma mark - 数据请求
-(void)getData
{
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/album/recommend/list/unlogin?device=iphone";
    
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        NSMutableArray *dataArray = [result objectForKey:@"frDatas"];
        
        for (NSMutableDictionary *dic in dataArray) {
            
            ZTFavorModel *model = [ZTFavorModel modelWithDictionary:dic];
            
            [self.recommendArray addObject:model];
            
        }
        [self.favorTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"请求失败  error");
    }];
}


#pragma mark 代理方法
// section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

// row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return self.myAlbumArray.count;
//        return 1;
    }
    else
    {
        return self.recommendArray.count;
    }
}


// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZTFavorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"favorTableCell"];
    
    if (indexPath.section == 0) {
        
        ZTFavorModel *model = [self.myAlbumArray objectAtIndex:indexPath.row];
        cell.model = model;
        cell.deleteButton.tag = [model.albumId integerValue];
        [cell.deleteButton setImage:[UIImage imageNamed:@"cell_delete_n@2x.png"] forState:UIControlStateNormal];
        [cell.deleteButton setImage:[UIImage imageNamed:@"cell_delete_h@2x.png"] forState:UIControlStateHighlighted];
        [cell.deleteButton addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    if (indexPath.section == 1) {
        
        ZTFavorModel *model = [self.recommendArray objectAtIndex:indexPath.row];
        cell.model = model;
        [cell.deleteButton setImage:[UIImage imageNamed:@"findcell_arrow@2x.png"] forState:UIControlStateNormal];

    }
    
    
    return cell;
}



#pragma mark - 删除按钮方法
- (void)deleteAction:(UIButton *)btn
{
    UIAlertController *alterC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alterC animated:YES completion:^{
       
        
    }];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[ZHFMDB shareFMDB] deleteByAlbumId:[NSString stringWithFormat:@"%ld", btn.tag]];
        self.myAlbumArray = [NSMutableArray arrayWithArray:[[ZHFMDB shareFMDB] selectAlbumData]];
        [self.favorTableView reloadData];

    }];
    
    UIAlertAction *channelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alterC addAction:channelAction];
    [alterC addAction:sureAction];
}

// header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return @"推荐收藏";
        
    }
    return @"我收藏的专辑";
}


// 点击cell触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        ZTAlbumDetailViewController *albumsDetailVC = [[ZTAlbumDetailViewController alloc] init];
        [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        albumsDetailVC.favorModel = ((ZTFavorModel *)self.myAlbumArray[indexPath.row]);
        albumsDetailVC.albumId = ((ZTFavorModel *)self.myAlbumArray[indexPath.row]).albumId;
        albumsDetailVC.myTitle = ((ZTFavorModel *)self.myAlbumArray[indexPath.row]).albumTitle;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    
    else if (indexPath.section == 1){
        
        ZTAlbumDetailViewController *albumsDetailVC = [[ZTAlbumDetailViewController alloc] init];
        
        [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        albumsDetailVC.favorModel = ((ZTFavorModel *)self.recommendArray[indexPath.row]);
        albumsDetailVC.albumId = ((ZTFavorModel *)self.recommendArray[indexPath.row]).albumId;
        albumsDetailVC.myTitle = ((ZTFavorModel *)self.recommendArray[indexPath.row]).albumTitle;
        
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
